package com.homework.controller;

import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.homework.dto.StudentDto;
import com.homework.entity.*;
import com.homework.pojo.Result;
import com.homework.service.*;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private AssignmentService assignmentService;
    @Autowired
    private StudentAssignmentService studentAssignmentService;
    @Autowired
    private StudentTeacherService studentTeacherService;

    /**
     * 获取所有学生
     *
     * @return
     */
    @GetMapping("students")
    public Result students() {
        return Result.success(studentService.list());
    }

    /**
     * 新增学生
     *
     * @param student（不传学生id）
     * @return
     */
    @PostMapping("student")
    public Result student(@RequestBody Student student) {
        student.setPassword(MD5.create().digestHex16("123456"));
        return Result.success(studentService.save(student));
    }

    /**
     * 修改学生信息
     *
     * @param student
     * @return
     */
    @PutMapping("student")
    public Result updateStudent(@RequestBody Student student) {
        return Result.success(studentService.updateById(student));
    }

    /**
     * 删除学生所有信息
     *
     * @param id 学生id
     * @return
     */
    @DeleteMapping("student/{id}")
    @Transactional
    public Result deleteStudent(@PathVariable Integer id) {
        studentTeacherService.remove(
                new LambdaQueryWrapper<StudentTeacher>()
                        .eq(StudentTeacher::getStudentId, id)
        );
        studentAssignmentService.remove(
                new LambdaQueryWrapper<StudentAssignment>()
                        .eq(StudentAssignment::getStudentId, id)
        );
        return Result.success(studentService.removeById(id));
    }

    /**
     * 获取老师列表
     *
     * @return
     */
    @GetMapping("teachers")
    public Result teachers() {
        return Result.success(teacherService.list());
    }

    /**
     * 新增老师
     *
     * @param teacher 不传teacherId
     * @return
     */
    @PostMapping("teacher")
    public Result teacher(@RequestBody Teacher teacher) {
        teacher.setPassword(MD5.create().digestHex16("123456"));
        return Result.success(teacherService.save(teacher));
    }

    /**
     * 修改老师
     *
     * @param teacher
     * @return
     */
    @PutMapping("teacher")
    public Result updateTeacher(@RequestBody Teacher teacher) {
        return Result.success(teacherService.updateById(teacher));
    }

    /**
     * 删除老师所有信息
     *
     * @param id 老师id
     * @return
     */
    @DeleteMapping("teacher/{id}")
    @Transactional
    public Result deleteTeacher(@PathVariable Integer id) {
        studentTeacherService.remove(
                new LambdaQueryWrapper<StudentTeacher>()
                        .eq(StudentTeacher::getTeacherId, id)
        );
        List<Integer> list = assignmentService.lambdaQuery()
                .eq(Assignment::getAssignedBy, id)
                .list().stream().map(Assignment::getAssignedBy).toList();
        System.out.println("list = " + list);
        if (!list.isEmpty())
            studentAssignmentService
                    .remove(new LambdaQueryWrapper<StudentAssignment>()
                            .in(StudentAssignment::getAssignmentId, list));
        assignmentService.remove(
                new LambdaQueryWrapper<Assignment>()
                        .eq(Assignment::getAssignedBy, id)
        );
        return Result.success(teacherService.removeById(id));
    }

}
